#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_hidden_music.h"
#include "api_spiflash_ex.h"

#if (MUSIC_HIDDEN_PLAY_EN)
SHiddenMusic sHiddMusic AT(.buf.func.music);

bool sd_hidden_music_init(void)
{
    bool boRet = true;
    if(!dev_is_online(DEV_SDCARD))
    {
        boRet = false;
    }
    if(boRet)
    {
        if(sd0_init())
        {
            sd_hidden_start_sector_init();
        }
        else
        {
            boRet = false;
        }
    }
    return boRet;
}

AT(.text.func.music)
void func_hidden_music_remove_device(u8 dev)
{
    if(f_msc.cur_dev == dev)
    {
        bsp_clr_mute_sta();
        if (dev_is_online(DEV_SDCARD) || dev_is_online(DEV_UDISK) || dev_is_online(DEV_SDCARD1))
        {
            f_msc.dev_change = 1;
            music_breakpoint_save();
        }
        else
        {
#if MUSIC_MODE_RETURN
            func_cb.sta = func_cb.last;
#else
            func_cb.sta = FUNC_NULL;
#endif
        }
    }
}

AT(.text.func.music)
void func_hidden_music_insert_device(u8 dev)
{
    sys_cb.cur_dev = dev;
    bsp_clr_mute_sta();
    f_msc.dev_change = 2;
    music_breakpoint_save();
}

AT(.text.func.music)
void func_sd_hidden_music_process(void)
{
    func_process();
    if(get_music_dec_sta() == MUSIC_STOP)
    {
        sd_hidden_music_switch_file(1);
    }
}

AT(.text.func.music)
void func_sd_hidden_music_display(void)
{

}

AT(.text.func.music)
void sd_hidden_music_switch_file(u8 direction)   //1 next , 0 prev
{
    music_control(MUSIC_MSG_STOP);
    // printf("direction = %d, cur_num = %d, total = %d\n",direction, sHiddMusic.cur_num, sHiddMusic.total_num);
    if(direction)
    {
        if(++sHiddMusic.cur_num > sHiddMusic.total_num)
        {
            sHiddMusic.cur_num = 1;
        }
    }
    else
    {
        if(sHiddMusic.cur_num >= 1)
        {
            sHiddMusic.cur_num--;
        }
        if(sHiddMusic.cur_num == 0)
        {
            sHiddMusic.cur_num = sHiddMusic.total_num;
        }
    }
    // printf("cur_num = %d, total = %d\n",sHiddMusic.cur_num, sHiddMusic.total_num);
    sd_hidden_music_num_kick(sHiddMusic.cur_num);
}

AT(.text.func.music)
bool sd_music_iamge_check(void)
{
    if(dev_is_online(DEV_SDCARD))
    {
        sHiddMusic.total_num = sd_music_get_file_total();
        // printf("sHiddMusic.total_num = %d\n", sHiddMusic.total_num);
        if(sHiddMusic.total_num > 0)
        {
            return true;
        }
    }
    return false;
}

AT(.text.func.music)
void func_sd_hidden_mp3_res_play(u32 addr, u32 len)
{
    msc_breakpiont_t brkpt;
    music_get_breakpiont(&brkpt);           //保存当前播放位置
    u32 cur_time = music_get_cur_time();
    music_control(MUSIC_MSG_STOP);
    register_spi_read_function(NULL);       //恢复内部SPI读接口.
    mp3_res_play(addr, len);
    register_spi_read_function(sd_hidden_read);
    music_set_cur_time(cur_time);
    sd_hidden_music_num_kick(sHiddMusic.cur_num);
    music_set_jump(&brkpt);                 //恢复播放位置
}

AT(.text.func.music)
static void func_sd_hidden_music_enter(void)
{
    msg_queue_clear();

    sd_hidden_music_init();
    if(!sd_music_iamge_check())
    {
        func_cb.sta = FUNC_NULL;
        return;
    }
    func_cb.mp3_res_play = func_sd_hidden_mp3_res_play;
    register_spi_read_function(sd_hidden_read);
    bsp_change_volume(sys_cb.vol);
    sHiddMusic.cur_num = 1;            //默认从1首开始播放
    sHiddMusic.pause = false;

    sd_hidden_music_num_kick(sHiddMusic.cur_num);
}

AT(.text.func.music)
static void func_sd_hidden_music_exit(void)
{
    music_control(MUSIC_MSG_STOP);
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_HIDDEN_MUSIC;
}

AT(.text.func.music)
void func_sd_hidden_music(void)
{
    printf("%s\n", __func__);
    func_sd_hidden_music_enter();
    while(func_cb.sta == FUNC_HIDDEN_MUSIC)
    {
        func_sd_hidden_music_process();
        func_sd_hidden_music_message(msg_dequeue());
        func_sd_hidden_music_display();
    }
    func_sd_hidden_music_exit();
}

#endif//(MUSIC_HIDDEN_PLAY_EN)

